import QtQuick 1.1
import com.nokia.symbian 1.1
import com.nokia.extras 1.1
import "core.js" as Core
import "settings.js" as Settings
import "date.js" as CustDate
import QtQuick 1.0

Page {
    id: mainPage


    property string title
    property string viewType;
    property string currency;
    property real fuelQtyTotal : 0;
    property real fuelExpTotal : 0;

   /* TitleHeader {
        id: listHeader
        text: "FuelPad"
    }*/

    function formatDate(date)
    {
        var d = Date.parse(date);
        return d.toString('dddd, MMMM ,yyyy');
    }

    Image{
        id:headImg
        x: 1
        y: 0
        width: 360
        height: 90



    source: "facts.png"

    Text {
        id: textFuelTotal
        x: 228
        y: 38
        width: 87
        height: 14
        color: "#c6b323"
        text:  fuelQtyTotal.toFixed(2) + " Liter"
        font.bold: true
        font.pixelSize: 18
    }

    Text {
        id: textFuelExp
        x: 181
        y: 64
        width: 87
        height: 14
        color: "#c6b323"
        text: fuelExpTotal.toFixed(2) + " "+currency
        z: 1
        font.pixelSize: 18
        font.bold: true
    }

    Text {
        id: text4
        x: 88
        y: 64
        color: "#ffffff"
        text: qsTr("Expenses :")
        font.pixelSize: 18
    }
    Text {
        id: text1
        x: 88
        y: 38
        color: "#ffffff"
        text: qsTr("Fuel Consumed :")
        font.pixelSize: 18
    }

    }

    ListView {
        id: itemView
        anchors.topMargin: 10
        anchors {
            left: mainPage.left; right: mainPage.right
            top: headImg.bottom; bottom:parent.bottom

        }        // set the modelview to boxModel


        model: fuelModel
        // use BoxItemDelegate to display boxes names via the boxModel
        delegate: FPListItem {
            id: itemDelegate

            qty: model.qty + " Liter"
            cost: model.cost+ " "+currency
            date:model.date
                onClicked: {
                // onClick we push the BoxPage to the stack
                mainPage.pageStack.push(Qt.resolvedUrl("DetailsView.qml"),{fuelId:model.id});

            }
            Component.onCompleted:
            {


            }
        }
        // we set clip to true that the out of view items clipped nicely
        clip: true
        highlightFollowsCurrentItem: false


    }
    onStatusChanged: {
        if(status == PageStatus.Activating) {
            updateUi();
        }
    }

    ListModel {
        id: fuelModel
    }
    function dummyData( model ) {
        fuelModel.clear()
        for (var i=0; i<10; i++) {
            fuelModel.append( { name: "todo ", qty: i, cost: i*10,date:"12/12/2010"  } );
        }
    }


    Component.onCompleted: {

    }

    FuelItemCreateDialog {
        id: fuelItemCreateDialog
        x: 1
        y: 0

        onAccepted: {

                var item = Core.defaultItem();

                item.cost = fuelCost;
                item.qty = fuelQty;
                item.date = fuelDate;
                Core.createFuelItem(item);
                updateUi();

        }
    }

    AboutPage
    {
        id:aboutPage
    }
    DeleteAllDialog
    {
        id: deleteAllDlg
        onAccepted: { updateUi()}
    }

    tools: ToolBarLayout {
        anchors.fill: parent
        ToolButton {iconSource: "toolbar-back"; onClicked: Qt.quit()}
        ToolButton {iconSource: "toolbar-add"; onClicked: fuelItemCreateDialog.open()}
        ToolButton {iconSource: "toolbar-search"; onClicked: launchSearch()}
        ToolButton {iconSource: "toolbar-refresh"; onClicked: updateUi()}
        ToolButton {iconSource: "toolbar-settings"; onClicked: pageStack.push(Qt.resolvedUrl("SettingsPage.qml"))}
        ToolButton {iconSource: "toolbar-view-menu"; onClicked:(appMenu.status == DialogStatus.Closed) ? appMenu.open() : appMenu.close() }

    }

    function launchSearch()
    {
        fuelExpTotal = 0.0;
        fuelQtyTotal = 0.0;

        var d = new Date();
        tDialog.year = d.getFullYear();
        tDialog.month = d.getMonth()+1;
       tDialog.day = d.getDate();


        tDialog.open()
    }


    DatePickerDialog {
        id: tDialog
        titleText: "Search by Date"
        acceptButtonText: "OK"
        rejectButtonText:"Cancel"


        onAccepted: {
            var strDt = tDialog.month+"/"+tDialog.day+"/"+tDialog.year;
            var d = new Date(strDt);
            var strDt =  CustDate.dateFormat(d, "mmm d, yyyy");
            Core.readFuelDate(fuelModel,strDt);
            fuelStats();
        }



    }
    Menu {
            id: appMenu
            visualParent: pageStack

            MenuLayout {
                MenuItem {text: "Delete All"; onClicked: { deleteAllDlg.open() } }
                MenuItem {text: "About"; onClicked: { aboutPage.open() } }
                MenuItem {text: "Help"; onClicked: {  }}

            }
    }


    function updateUi()
    {
        itemView.model = 0;
        var qtyTotal = 0;
        var costTotal = 0;

        fuelExpTotal = 0;
        fuelQtyTotal = 0;

       // Core.readFuelDate(fuelModel,new Date());
        Settings.initialize();
        var temp = Settings.getSetting("viewType");
        if(temp === "0")
        {
            viewType = "All Time";
        }
        else
        {
            viewType = temp;
        }

        temp = Settings.getSetting("currency");
        if(temp === "0")
        {
            currency = "USD";
        }
        else
        {
            currency = temp;
        }


        Core.reOpenDB();


        if(viewType === "All Time")
        {
            Core.readAllFuel(fuelModel);
        }
        else if(viewType === "Today")
        {
            var dt;
            var d = new Date();
            dt = d.getMonth()+"/"+d.getDay()+"/"+d.getFullYear();
            var strDt =  CustDate.dateFormat(d, "mmm d, yyyy");

            Core.readFuelDate(fuelModel,strDt);
        }

        /*var dt;
        var d = new Date();
        dt = d.getDate()+"/"+(d.getMonth()+1)+"/"+d.getFullYear();

        Core.readFuelDate(fuelModel,dt); */
        itemView.model = fuelModel;

        fuelStats();
    }
    function fuelStats()
    {
         var temp = 0;
         var temp1 = 0;
        fuelQtyTotal = 0.0;
        fuelExpTotal = 0.0;

        for(var i =0; i < fuelModel.count; i++)
        {


            temp = parseFloat(fuelModel.get(i).qty);
            if(isNaN(temp) === false)
                fuelQtyTotal += temp;

            temp = parseFloat(fuelModel.get(i).cost);
            if(isNaN(temp) === false)
                fuelExpTotal += temp;

            console.debug(fuelQtyTotal);
        }

    }


}
